const puppeteer = require('puppeteer');
const useProxy = require("@lem0-packages/puppeteer-page-proxy");
const request = require('request');
const file = require('fs');
const yaml = require('js-yaml');
const os = require('os');

let browser  = null;
let noteList = [];
let agent = null;

async function delay(time) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve();
        }, time);
    });
}

async function run() {
    const config = await getConfig();
    const platform = os.platform();  

    let options;
    if (platform === "darwin") {
        options = {
            args: ['--disable-web-security'], 
            headless: false,
            defaultViewport: {
                width: 1920,
                height: 1080
            }
        }
    } else {
        options = {
            args: ['--disable-web-security'],
            headless: false,
            defaultViewport: {
                width: 1920,
                height: 1080
            },
            executablePath: config.chromePath ?? ''
        }
    }

    // 启动浏览器
    browser = await puppeteer.launch(options);
    // 打开新页面
    const page = await browser.newPage();
    await useProxy(page,  await getProxyIp());
    // 设置缓存选项
    await page.setCacheEnabled(true);
    await page.setUserAgent(agent);
}

async function getConfig() {
    const yamlContent =  file.readFileSync('confing.ymal', {encoding: 'utf-8'});
    return yaml.load(yamlContent);
}

async function getCookies() {
    // 读取缓存里的Cookies信息
    let cookies = {};
    let cookiesString = file.readFileSync('./cache/cookies.json', {encoding: 'utf-8'});
    //cookies不为空
    if (cookiesString.length > 4) {
        cookies = JSON.parse(cookiesString);
    }
    return cookies;
}

async function saveCookies(page) {

    try {
        //登录后保存Cookies
        cookies = await page.cookies('https://www.xiaohongshu.com'); 
        if(cookies!=null){ 
            //将cookies保存到文件
            const cookiesString = JSON.stringify(cookies, null, 2);  
            let fileStream;
            try {
                fileStream = file.createWriteStream('./cache/cookies.json');
                fileStream.write(cookiesString);
                // 确保在结束之前关闭文件流
                fileStream.end();
            } catch (error) {
                console.error('Error opening file:', error);
            }
        } 
        console.log('cookies content:', cookies);
    } catch (error) {
        console.log('cookies error:', error);
    }
}

async function runTodo(data) {

    try {

        const url = data.url
        const noteId = data.note_id
        let pageStat = false; 
        let pageRefush = 0; 
       
        // 打开新页面 
        page = await browser.newPage();
        await page.setUserAgent(agent);
        
        // 添加headers
        const headers = {
            'Accept-Encoding': 'gzip' // 使用gzip压缩让数据传输更快
        };

        // 设置缓存选项
        await page.setCacheEnabled(true);
        await page.setExtraHTTPHeaders(headers);
        await page.setUserAgent(await getUserAgent());
        //cookies绑定级定到page
        const parsedCookies = await getCookies();
        if (parsedCookies.length > 0) {
            //将Cookies加载到页面
            await page.setCookie(...parsedCookies);
        }else{
            
        }

        //启用请求拦截
        await page.setRequestInterception(true); 

        page.on('request', async request => { 

            if(request.url()==="https://pages.xiaohongshu.com/data/xhs_pc_nps"){
                if (request.isInterceptResolutionHandled()) return;
                request.abort();
            }
            
            else if(request.url()==="https://edith.xiaohongshu.com/api/sns/web/v1/note/metrics_report" || request.url()==="https://edith.xiaohongshu.com/api/sns/web/racing_report"){
                if (request.isInterceptResolutionHandled()) return;
                request.respond({
                    status: 200,
                    headers: {
                        'Access-Control-Allow-Origin':'https://www.xiaohongshu.com',
                        'Access-Control-Allow-Credentials':true,
                        'Access-Control-Allow-Headers': 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Client-Build,X-Client-Platform,X-Client-Version,X-Mx-ReqToken,X-Requested-With,X-Sign,Batch,request-from,Biz-Type',
                        'Access-Control-Allow-Methods': 'GET,POST,OPTIONS,HEAD',
                    },
                    contentType: 'text/json; charset=utf-8',
                    body: JSON.stringify({"data":{"success":true},"code":0,"success":true,"msg":"成功"}                    )
                }); 
            }
            else if(request.url() === "https://www.xiaohongshu.com/api/sec/v1/scripting"){
                if (request.isInterceptResolutionHandled()) return;
                request.respond({
                    status: 200,
                    headers: {
                        'Access-Control-Allow-Origin':'*',
                        'Access-Control-Allow-Credentials':true,
                        'Access-Control-Allow-Headers': 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Client-Build,X-Client-Platform,X-Client-Version,X-Mx-ReqToken,X-Requested-With,X-Sign,Batch,request-from,Biz-Type',
                        'Access-Control-Allow-Methods': 'GET,POST,OPTIONS,HEAD',
                    },
                    contentType: 'text/json; charset=utf-8',
                    body: JSON.stringify({"data":"debugger;","code":0,"success":true,"msg":"成功"})
                });
            }
            else if(request.url() === "https://www.xiaohongshu.com/api/sec/v1/scripting"){
                if (request.isInterceptResolutionHandled()) return;
                request.respond({
                    status: 200,
                    headers: {
                        'Access-Control-Allow-Origin':'*',
                        'Access-Control-Allow-Credentials':true,
                        'Access-Control-Allow-Headers': 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Client-Build,X-Client-Platform,X-Client-Version,X-Mx-ReqToken,X-Requested-With,X-Sign,Batch,request-from,Biz-Type',
                        'Access-Control-Allow-Methods': 'GET,POST,OPTIONS,HEAD',
                    },
                    contentType: 'text/json; charset=utf-8',
                    body: JSON.stringify({"data":"debugger;","code":0,"success":true,"msg":"成功"})
                });
            }
            else if(request.url()==="https://apm-fe.xiaohongshu.com/api/data" || request.url()==="https://t2.xiaohongshu.com/api/v2/collect"){
                if (request.isInterceptResolutionHandled()) return;
                request.respond({
                    status: 200,
                    headers: {
                        'Access-Control-Allow-Origin':'*',
                        'Access-Control-Allow-Credentials':true,
                        'Access-Control-Allow-Headers': 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Client-Build,X-Client-Platform,X-Client-Version,X-Mx-ReqToken,X-Requested-With,X-Sign,Batch,request-from,Biz-Type',
                        'Access-Control-Allow-Methods': 'GET,POST,OPTIONS,HEAD',
                    },
                    contentType: 'text/json; charset=utf-8',
                    body: JSON.stringify({"code":0,"msg":"Success","success":true})
                });
            }else {
                await useProxy(request, await getProxyIp()); 
                if (request.isInterceptResolutionHandled()) return;
                request.continue();
            }
        });

        await page.on('response', async response => {  
 
            //await saveCookies(page) 
            const config = await getConfig();

            // 替换为你要监听的异步请求URL
            if (response.url().includes('https://edith.xiaohongshu.com/api/sns/web/v2/comment/page')) {
                 
                if (response.frame() != null) {

                    pageRefush++; 
            
                    if(response.ok){ 

                        try{ 
                        
                        const data = await response.json();  
                        console.log('评论列表:', data, response.url()); 

                        let commentData = {}
                        commentData = data.data
                        commentData.note_id = noteId

                        let pageUrl = 'http://m.tikshop.asia/api/comment/add.html';
                        const resp = await fetch(pageUrl, {
                            method: 'POST',
                            headers: {'Content-Type': 'application/json', 'Auth-Token': config.token},
                            body: JSON.stringify(commentData)
                        });

                        //获得响应对象
                        if (resp.ok) {
                            const respJson = await resp.json()
                            console.log("评论成功:", respJson)
                        }else {
                            console.log("扫码登录", resp)
                        }

                        //加载到最后页
                        if (data.data.has_more === false || pageRefush >= 1 || data.code===300013 || data.code<0 ) { 
                            
                            setTimeout( async ()=>{    
                                if(noteList.length===0){
                                    await getNoteList()
                                }
        
                                //取出数据
                                let note = noteList.pop();
                                //新跑业务
                                runTodo(note); 
                                page.close();

                            }, 1000) 
                        }

                        //滚动到div底部 
                        await page.evaluate(`function bb () { 
                            //设置一个每200毫秒执行一次的定时器
                            setTimeout(function () {
                                const div = document.querySelector('div.note-scroller');
                                div.scrollTop = 0;
                                div.scrollTop = div.scrollHeight * 1.2; 
                                window.addEventListener("error",function name(params) {
                                    console.log("aaa");
                                });
                            }, 1000);
                        }
                        bb();
                        `);

                        }catch(e){
                            console.log(e)

                             //取出数据
                             let note = noteList.pop();
                             //新跑业务
                             runTodo(note); 
                             page.close();  
                        }

                    }else{

                        //取出数据
                        let note = noteList.pop();
                        //新跑业务
                        runTodo(note); 
                        page.close(); 
                    }
                }
            }
        });

        await page.goto(url);
    
        //获取当前页面的window对象
        const windowObject = await page.evaluate(() => {
            setTimeout(()=>{
                 let windowObject2 = window.__INITIAL_STATE__.note
            },
            1000)
        });

        const post = await page.$eval('#detail-title', element => {
            return {
                title: element.textContent, // 获取元素的文本内容
                content: element.nextElementSibling.textContent // 获取元素的下一个兄弟元素（正文内容元素）的文本内容
            };
        });

        if(post.title.length > 0) {
        
            const config = await getConfig();
            data.title = post.title
            data.content = post.content
            const pageUrl = 'http://m.tikshop.asia/api/comment/update.html';
            const response = await fetch(pageUrl, {
                method: 'POST',
                headers: {'Content-Type': 'application/json', 'Auth-Token': config.token},
                body: JSON.stringify(data)
            });
            //获得响应对象
            if (response.ok) {
                console.log("标题成功:", await response.json())
            }
        }

        //滚动到div底部
        await page.evaluate(
            `function aa () {
                //设置一个每200毫秒执行一次的定时器
                setInterval(function () {
                     console.log(window.location.href)
                },2000);
            }
            aa()`
        );

    }catch (e){
        console.log(e.message)
    }
}

async function getNoteList() {
    const pageUrl = 'http://m.tikshop.asia/api/note/index.html?p=1';
    const config = await getConfig();   
    const response = await fetch(pageUrl, {
        method: 'GET',
        headers: {'Content-Type': 'application/json', 'Auth-Token': config.token},
    })
    const noteData = await response.json()
    if(noteData.code===1){
        noteList = noteData.data.rows??[];
        console.log("noteList:", pageUrl) 
        console.log("noteList:", noteList.length)
    }else{
        console.log("noteList:", noteData)
    }
}

async function getProxyIp() {   
    const config = await getConfig();  
    const proxy = config.proxy; 
    return proxy;
}

async function getUserAgent() {
    const agent = [
        "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36",
        "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2226.0 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.4; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2225.0 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2225.0 Safari/537.36",
        "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2224.3 Safari/537.36",
        "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36",
        "Mozilla/5.0 (Windows NT 4.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.67 Safari/537.36",
        "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.67 Safari/537.36",
        "Mozilla/5.0 (X11; OpenBSD i386) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1944.0 Safari/537.36",
        "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.3319.102 Safari/537.36",
        "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2309.372 Safari/537.36",
        "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2117.157 Safari/537.36",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36",
        "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1866.237 Safari/537.36",
        "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/4E423F",
        "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36 Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10",
        "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.517 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1664.3 Safari/537.36",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1664.3 Safari/537.36",
        "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1623.0 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.17 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.62 Safari/537.36",
        "Mozilla/5.0 (X11; CrOS i686 4319.74.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1468.0 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1467.0 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1464.0 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1500.55 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36",
        "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.90 Safari/537.36",
        "Mozilla/5.0 (X11; NetBSD) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36",
        "Mozilla/5.0 (X11; CrOS i686 3912.101.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36",
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.60 Safari/537.17",
        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1309.0 Safari/537.17",
        "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.15 (KHTML, like Gecko) Chrome/24.0.1295.0 Safari/537.15",
        "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.14 (KHTML, like Gecko) Chrome/24.0.1292.0 Safari/537.14"
    ];
    const randomIndex = Math.floor(Math.random() * agent.length);
    return agent[randomIndex];
}

async function executeAsync() {  

    agent = await getUserAgent(); 

    //获取主列表
    await getNoteList();
    //运行浏览器
    await run().then( async  () => {
        //取出数据
        const note = noteList.pop();
        //运行主程序
        await runTodo(note)
    }).catch(
        error => console.error('出错:', error)
    );
}

executeAsync();
